<template>
    <n-form ref="formRef" label-placement="left" :label-width="100" :model="form">
        <jb-upload
            v-model="form.files"
            :multiple="true"
            save-path="sysnotice/{date(yyMMdd)}/{random(6)}/{ori}"
        >
            <n-button>上传文件</n-button>
        </jb-upload>
    </n-form>
</template>
<script setup lang="ts">
import { onMounted, ref } from 'vue'
import type { FormInst } from 'naive-ui'
import { JBoltApi } from '@/utils/request'
import { useResetableData } from '@/hooks/common/use-resetable-data'
import { ResData } from '@/typings/request'

/** 表单相关 start */
const formRef = ref<FormInst | null>()
const props = withDefaults(
    defineProps<{
        id: string
    }>(),
    {
        id: ''
    }
)

/** 表单相关 start */
interface InformType {
    id: string
    title: string
    type: number
    files: string
    content: string
    priorityLevel: number
    receiverType: number
    receiverValue: string
}
const form = useResetableData<InformType>({
    id: '',
    title: '',
    type: 1,
    content: '',
    files: '',
    priorityLevel: 1,
    receiverType: 1,
    receiverValue: ''
})

/**
 * 提交表单
 */
async function submit() {
    await formRef.value?.validate()
    let url = props.id ? '/api/admin/sysNotice/update' : '/api/admin/sysNotice/save'
    await JBoltApi.tryPost(url, form)
    return true
}

function loadEditData() {
    JBoltApi.get<ResData>(`/api/admin/sysNotice/${props.id}`).then(
        ({ error, result }) => {
            if (error) return
            form._reset(result.data)
        }
    )
}

onMounted(() => {
    if (props.id) {
        loadEditData()
    }
})

defineExpose({
    submit
})

/** 表单相关 end */
</script>
<style scoped></style>
